package com.wxy;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 47868
 */
@SpringBootApplication
@EnableConfigurationProperties
@EnableAsync
public class SpringBootDemoApplication implements CommandLineRunner {
    private ConfigurableEnvironment environment;
    @Autowired
    public void setEnvironment(ConfigurableEnvironment environment) {
        this.environment = environment;
    }
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }

    @Override
    public void run(String... args) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("app.message","zhangsan");
        MapPropertySource source = new MapPropertySource("map", map);
        /*修改app.message的信息*/
        environment.getPropertySources().addFirst(source);
        String message=environment.getProperty("app.message");
        System.out.println(message);
    }
    @Bean
    public ElasticsearchClient client() {
        // 创建底层 REST 客户端（无认证）
        RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200, "http")
        ).build();

        // 创建传输层并配置 JSON 映射器
        ElasticsearchTransport transport = new RestClientTransport(
                restClient,
                new JacksonJsonpMapper()
        );

        // 创建新的高级客户端
        return new ElasticsearchClient(transport);
    }
}
